package com.kfc.sales;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.Vector;


import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.net.ParseException;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.TextView;

public class ViewCompany extends ListActivity{
	TextView companyHeader;
	EditText companyID;
	EditText companyAddress;
	EditText companySales;
	Calendar currentDate = Calendar.getInstance();
	SimpleDateFormat formatter= new SimpleDateFormat("MMM/dd/yyyy");
	String dateNow = formatter.format(currentDate.getTime());
	
	private LayoutInflater mInflater;
	private Vector<RowData> data;
	RowData rd;

	static final String[] title = new String[] {
		    "Bucket Meal", "Chicken Bowl",	"Hotshots","Krushers", "Mashed Potato", "One-Piece Chicken", "Spaghetti", "Tiramisu", "Twister", "Zinger"    	};


static final String[] branchid = new String[] {
		"2", "53", "132", "26", "3", "19", "132", "72", "65", "12"	};

static final String[] sales = new String[] {
	"12345", "12345", "12345", "12345", "12345", "12345", "12345", "12345", "12345", "12345"	};



	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.viewcompany);
        
        companyID = (EditText) findViewById(R.id.compID);
        companyAddress = (EditText) findViewById(R.id.compLocation);
        companySales = (EditText) findViewById(R.id.compSales);
        companyHeader = (TextView) findViewById(R.id.viewcompanyheader);
		Bundle extras = getIntent().getExtras();
		String value1 = extras.getString("BranchID");
		String value2 = extras.getString("BranchLoc");
		String value3 = extras.getString("TotalSales");
		companyID.setText(value1);
		companyAddress.setText(value2);
		companySales.setText(value3);
		companyHeader.setText("Branch Report as of: "+dateNow);
		mInflater = (LayoutInflater) getSystemService(
				Activity.LAYOUT_INFLATER_SERVICE);
		
		
		data = new Vector<RowData>();
		
		for(int i=0;i<title.length;i++){
			try {
			 	rd = new RowData(title[i],branchid[i],sales[i]);
			    } catch (ParseException e) {
			    	e.printStackTrace();
			   }
			   data.add(rd);
		}
		
		CustomAdapter adapter = new CustomAdapter(this, R.layout.lists,
		                                     R.id.branchloc, data);
		
		setListAdapter(adapter);
		getListView().setTextFilterEnabled(true);
        
	}
	
    public class RowData {
	       protected String mProductName;
	       protected String mProductQty;
	       protected String mSales;
	       
	       RowData(String location,String brid,String brsales)
	       {
		       mProductName=location;
		       mProductQty = brid;
		       mSales=brsales; 
	       }
	
	}
	  
    public class CustomAdapter extends ArrayAdapter<RowData> {
	  public CustomAdapter(Context context, int resource,
	                        int textViewResourceId, List<RowData> objects) {               
	 super(context, resource, textViewResourceId, objects);
	}
	      @Override
	       public View getView(int position, View convertView, ViewGroup parent) {   
	       ViewHolder holder = null;
	       TextView prodname = null;
	       TextView prodsales=null;
	       TextView prodqty = null;
	       RowData rowData= getItem(position);
	       if(null == convertView){
	            convertView = mInflater.inflate(R.layout.saleslist, null);
	            holder = new ViewHolder(convertView);
	            convertView.setTag(holder);
	        }
	             holder = (ViewHolder) convertView.getTag();
	             prodname = holder.getProductName();
	             prodname.setText("Product Name: "+rowData.mProductName);                                                     
	             prodsales=holder.getProductsales();
	             prodsales.setText("Sales: "+rowData.mSales);
	             prodqty = holder.getProductQuantity();
	             prodqty.setText("Quantity Sold:"+rowData.mProductQty);    
	             return convertView;
	       }
	            private class ViewHolder {
	            private View mRow;
	            private TextView productName = null;
	            private TextView prodSales=null; 
	            private TextView productQuantity = null;
	            public ViewHolder(View row) {
	            mRow = row;
	 }
	         public TextView getProductName() {
	             if(null == productName){
	                 productName = (TextView) mRow.findViewById(R.id.prodname);
	                }
	            return productName;
	         }     
	         public TextView getProductsales() {
	             if(null == prodSales){
	            	 prodSales = (TextView) mRow.findViewById(R.id.prodsale);
	                    }
	           return prodSales;
	         }
	        public TextView getProductQuantity() {
	             if(null == productQuantity){
	            	 productQuantity = (TextView) mRow.findViewById(R.id.prodqty);
	                                      }
	                return productQuantity;
	        }
	     }
	   }

}
